package erfgame.core.world.generator.terrain;

public class SinusoidalTerrainBlockGenerator implements TerrainBlockGenerator {

	private int waveLength;
	private int waveHeight;
	private int meanDepth;
	private byte terrainAboveType;
	private byte terrainBelowType;
	
	public SinusoidalTerrainBlockGenerator( int waveLength, int waveHeight, int meanDepth, byte terrainAboveType, byte terrainBelowType ) {
		this.waveLength = waveLength;
		this.waveHeight = waveHeight;
		this.meanDepth = meanDepth;
		this.terrainAboveType = terrainAboveType;
		this.terrainBelowType = terrainBelowType;
	}
	
	public boolean generate( int bx, int by, int bz, int bw, int bh, int bd, byte[][][] terrain ) {
		for( int x = bw; x>0; ) {
			x--;
			int tx = x + bx * bw;
			double sx = ((double)tx)/((double)this.waveLength) * Math.PI * 2;
			for( int y=bh; y>0; ) {
				y--;
				int ty = y + by * bh;
				double sy = ((double)ty)/((double)this.waveLength) * Math.PI * 2;
				// work out the height at this point
				int zmin = this.meanDepth + (int)Math.round( ( Math.sin( sy + Math.PI/2 ) * Math.sin( sx ) ) * this.waveHeight);
				for( int z=bd; z>0; ) {
					z--;
					int tz = z + bz * bd;
					byte terrainType;
					if( tz > zmin ) {
						terrainType = this.terrainBelowType;
					} else {
						terrainType = this.terrainAboveType;
					}
					terrain[x][y][z] = terrainType;
				}
			}
		}
		return true;
	}

}
